package top.ruanlitao.shortlink.admin.config;

import feign.RequestInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import top.ruanlitao.shortlink.admin.common.biz.user.UserContext;
import top.ruanlitao.shortlink.admin.common.constant.UserConstant;

/**
 * openFeign 微服务调用传递用户信息配置
 */
@Configuration
public class OpenFeignConfiguration {

    /**
     * 将用户信息加入到请求头中
     */
    @Bean
    public RequestInterceptor requestInterceptor() {
        return template -> {
            template.header(UserConstant.KEY_USERNAME, UserContext.getUsername());
            template.header(UserConstant.KEY_USER_ID, UserContext.getUserId());
            template.header(UserConstant.KEY_REAL_NAME, UserContext.getRealName());
        };
    }
}